package top.kaoshanji.p1ch08a;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * author kaoshanji
 * ^_^
 * create time 2025/2/13 10:55
 */
public class CreatingCollections {

    private static final Logger logger = LogManager.getLogger(CreatingCollections.class);

    public static void main(String[] args) {

        // 代码清单-p1ch08a-176页a：集合工厂
        logger.info("...集合工厂...");
        creatingList();


    }

    private static void creatingList() {
        System.out.println("------ Creating Lists ------");

        System.out.println("--> Creating a list the old-fashioned way");
        List<String> friends = new ArrayList<>();
        friends.add("Raphael");
        friends.add("Olivia");
        friends.add("Thibaut");
        System.out.println(friends);

        System.out.println("--> Using Arrays.asList()");
        List<String> friends2 = Arrays.asList("Raphael", "Olivia");
        friends2.set(0, "Richard");
        System.out.println(friends2);
        try {
            friends2.add("Thibaut");
            System.out.println("We shouldn't get here...");
        } catch (UnsupportedOperationException e) {
            System.out.println("As expected, we can't add items to a list created with Arrays.asList().");
        }

        System.out.println("--> Creating a Set from a List");
        Set<String> friends3 = new HashSet<>(Arrays.asList("Raphael", "Olivia", "Thibaut"));
        System.out.println(friends3);

        System.out.println("--> Creating a Set from a Stream");
        Set<String> friends4 = Stream.of("Raphael", "Olivia", "Thibaut").collect(Collectors.toSet());
        System.out.println(friends4);


    }

}
